﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MyBlog2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MyBlog2.ViewModels;
using System.Linq.Expressions;

namespace MyBlog2.Repository
{
    public class ArticleClassifyRepository : IArticleClassifyRepository
    {
        private readonly AppDbContext _context;

        public ArticleClassifyRepository(AppDbContext context)
        {
            this._context = context;
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="articleClassify"></param>
        /// <returns></returns>
        public ArticleClassify AddArticleClassify(ArticleClassify articleClassify)
        {
            _context.ArticleClassifys.Add(articleClassify);
            _context.SaveChanges();
            return articleClassify;
        }


        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteArticleClassify(int id)
        {
            var classify = _context.ArticleClassifys.FirstOrDefault(c=>c.Id == id);
            if (classify == null)
            {
                return false;
            }
            _context.ArticleClassifys.Remove(classify);
            return _context.SaveChanges() > 0;
        }


        /// <summary>
        /// 编辑分类
        /// </summary>
        /// <param name="editArticleClassify">新分类对象</param>
        /// <returns></returns>
        public ArticleClassify EditArticleClassify(ArticleClassify editArticleClassify)
        {
            var classify = _context.ArticleClassifys.FirstOrDefault(a=>a.Id == editArticleClassify.Id);
            if (classify != null)
            {
                classify.Name = editArticleClassify.Name;
                classify.Sort = editArticleClassify.Sort;

                _context.ArticleClassifys.Update(classify);
                _context.SaveChanges();
                return classify;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取所有分类(排序值排序)
        /// </summary>
        /// <returns></returns>
        public List<ArticleClassify> GetArticleClassifieList()
        {
            return _context.ArticleClassifys.OrderBy(a=>a.Sort).ToList();
        }

        /// <summary>
        /// 获取所有分类(文章数量排序)
        /// </summary>
        /// <param name="nouse"></param>
        /// <returns></returns>
        public List<ArticleClassify> GetArticleClassifieList(string nouse)
        {
            return _context.ArticleClassifys.Include(a=>a.Articles).OrderByDescending(a => a.Articles.Count).ToList();
        }



        /// <summary>
        /// 获取一个分类
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public  ArticleClassify GetOneArticleClassify(int id)
        {
            return _context.ArticleClassifys.FirstOrDefault(a=>a.Id == id);
        }
    }
}
